package com.example.quanlithuchi;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import Adapter.QuanLiNoAdapter;
import InterFaceIModel.IQuanLiNo;
import Presenter.QuanliNo;
import View.IQuanliNoView;
import View.IQuanlino_Edit;
import ViewHoder.NoHoder;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class QuanLiNoActivity extends Activity implements IQuanliNoView,IQuanlino_Edit {

	Context context=this;
	Button btnAdd;
	QuanliNo quanliNo=null;
	QuanLiNoAdapter adapter;
	ListView ListNo;
	
	IQuanLiNo quanlino_edit;
	IQuanlino_Edit editquanlino=this;
	
	//edittext
	EditText txtNgaytra;
	EditText txtHoten;
	EditText txtSotien;
	EditText txtDienthoai;
	EditText txtNgayNo;
	EditText txtGhichu;
	int day,year,month;
	long ngaytra=0,ngayno=0;
	int day_no,month_no,year_no,day_tra,month_tra,year_tra;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_quan_li_no);
		setTitle("Quan li no");
		if (quanliNo==null) {
			 quanliNo=new QuanliNo(this);
		}
		ListNo=(ListView) findViewById(R.id.ListNo);
		
		//quanliNo.LoadAll();
		quanliNo.LoadAll();
		btnAdd=(Button) findViewById(R.id.btnAddNo);
		btnAdd.setOnClickListener(onAddClickListener);
		ListNo.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				NoHoder hoder=(NoHoder) view.getTag();
				ImageView btnDelete=hoder.btnDelete;
				quanlino_edit =(IQuanLiNo) btnDelete.getTag();
				btnDelete.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						int x=quanliNo.Delete(quanlino_edit);
						if (x>0) {
							
							adapter.remove(quanlino_edit);
							quanlino_edit=null;
							adapter.notifyDataSetChanged();
						}
					}
				});
				
				ImageView btnEdit=hoder.btnEdit;
				btnEdit.setOnClickListener(onAddClickListener);
			}
		});
		
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_quan_li_no, menu);
		return true;
	}

	@Override
	public void SetListNo(List<IQuanLiNo> listNo) {
		// TODO Auto-generated method stub
		if (listNo!=null&&listNo.size()>0) {
			
			adapter=new QuanLiNoAdapter(context, R.layout.no_item_list, listNo);
			if (ListNo!=null) {
				ListNo.setAdapter(adapter);

			}else {
				Log.d("listview is", "list is null");
			}
			
		}else {
			listNo=new ArrayList<IQuanLiNo>();
			adapter=new QuanLiNoAdapter(context, R.layout.no_item_list, listNo);
			ListNo.setAdapter(adapter);
		}
		
	}

	@Override
	public Context getContext() {
		// TODO Auto-generated method stub
		return context;
	}
	OnClickListener onAddClickListener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			AlertDialog.Builder builder=new AlertDialog.Builder(context);
			builder.setTitle("Add No");
			View view=getLayoutInflater().inflate(R.layout.edit_no, null);
			//get all control
			txtNgaytra=(EditText) view.findViewById(R.id.txtngaytra_no);
			txtNgaytra.setOnTouchListener(onTouchListener);
			txtDienthoai=(EditText) view.findViewById(R.id.txtSodienthoai_no);
			txtNgayNo=(EditText) view.findViewById(R.id.txtngayvay_no);
			txtNgayNo.setOnTouchListener(onTouchListener);
			txtHoten=(EditText) view.findViewById(R.id.txtHoten_no);
			txtSotien=(EditText) view.findViewById(R.id.txtSotien_no);
			txtGhichu=(EditText) view.findViewById(R.id.txtghichu_no);
			quanliNo.Edit(editquanlino, quanlino_edit);
			builder.setView(view);
			builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					if (quanlino_edit==null) {
						IQuanLiNo  results=quanliNo.Insert(editquanlino);
						if (results==null) {
							
							Log.d("canh bao", "that bai");
						}else {
							adapter.add(results);
							adapter.notifyDataSetChanged();
						}
					}else {
						//edit
						
						IQuanLiNo  results=quanliNo.Update(editquanlino);
						if (results==null) {
							
							Log.d("canh bao", "that bai");
						}else {
							int index=adapter.getPosition(quanlino_edit);
							adapter.remove(quanlino_edit);
							adapter.insert(results, index);
							quanlino_edit=results;
							
							adapter.notifyDataSetChanged();
						}
					}
				}
			});
			builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			});
			AlertDialog dialog=builder.create();
			dialog.show();

			
		}
	};
	OnTouchListener onTouchListener=new OnTouchListener() {
		
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.txtngayvay_no:
			{
				
				showDialog(1);
			}break;
			case R.id.txtngaytra_no:
			{
				showDialog(2);
			}break;
				

			
			}
			return false;
		}
	};
//implement inteface IQuanlino_Edit
	@Override
	public void setHoten(String hoten) {
		// TODO Auto-generated method stub
		txtHoten.setText(hoten);
	}

	@Override
	public void setSodienthoai(String sodienthoai) {
		// TODO Auto-generated method stub
		txtDienthoai.setText(sodienthoai);
	}

	@Override
	public void setSotien(double sotien) {
		// TODO Auto-generated method stub
		txtSotien.setText(String.valueOf(sotien));
	}

	@Override
	public void setNgayNo(long ngayno) {
		// TODO Auto-generated method stub
		Date ngayNoDate=new Date(ngayno);
		day_no=ngayNoDate.getDate();
		month_no=ngayNoDate.getMonth();
		year_no=ngayNoDate.getYear()+1900;
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		String ngaynoString = formatter.format(ngayNoDate);
		txtNgayNo.setText(ngaynoString);
	}

	@Override
	public void setNgayTra(long ngaytra) {
		// TODO Auto-generated method stub
		Date ngaytraDate=new Date(ngaytra);
		
		day_tra=ngaytraDate.getDate();
		month_tra=ngaytraDate.getMonth();
		year_tra=ngaytraDate.getYear()+1900;
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		String ngaynoString = formatter.format(ngaytraDate);
		txtNgaytra.setText(ngaynoString);
		
	}

	@Override
	public void setGhichu(String ghichu) {
		// TODO Auto-generated method stub
		txtGhichu.setText(ghichu);
	}

	@Override
	public String getHoten() {
		// TODO Auto-generated method stub
		return txtHoten.getText().toString();
	}

	@Override
	public String getSodienthoai() {
		// TODO Auto-generated method stub
		return txtDienthoai.getText().toString();
	}

	@Override
	public double getSotien() {
		// TODO Auto-generated method stub
	try {
		return Double.parseDouble(txtSotien.getText().toString());
	} catch (Exception e) {
		Toast.makeText(context, "input String incorrect format", Toast.LENGTH_LONG).show();
		return 0;
	}
		
	}

	@Override
	public long getNgayNo() {
		// TODO Auto-generated method stub
		return ngayno;
	}

	@Override
	public long getNgayTra() {
		// TODO Auto-generated method stub
		return ngaytra;
	}

	@Override
	public String getGhiChu() {
		// TODO Auto-generated method stub
		return txtGhichu.getText().toString();
	}

	@Override
	public int getId() {
		// TODO Auto-generated method stub
		return quanlino_edit.getId();
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		final int x=id;
		if (quanlino_edit==null) {
			Calendar calendar=Calendar.getInstance();
			day=calendar.get(Calendar.DAY_OF_MONTH);
			month=calendar.get(Calendar.MONTH);
			year=calendar.get(Calendar.YEAR);
			
		}else {
			if (x==1) {
				
				day=day_no;
				month=month_no;
				year=year_no;
			}else {
				day=day_tra;
				month=month_tra;
				year=year_tra;
			}
		}
		
		
			return new DatePickerDialog(context, new OnDateSetListener() {
				
				@Override
				public void onDateSet(DatePicker view, int y, int monthOfYear,
						int dayOfMonth) {
					// TODO Auto-generated method stub
					
					year=y;
					month=monthOfYear;
					day=dayOfMonth;
					Date date=new Date(year-1900, month, day);
					SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
					String formattedDate = formatter.format(date);
					switch (x) {
					case 1:
					{
						txtNgayNo.setText(formattedDate);
						ngayno=date.getTime();
						
					}
						break;
					case 2:
					{
						txtNgaytra.setText(formattedDate);
						ngaytra=date.getTime();
					}
						break;

					
					}
					
				}
			}, year, month, day);
		
		
	}
	
    
}
